package com.unicom.vw.modules.sys.controller;

import com.unicom.vw.common.annotation.SysLog;
import com.unicom.vw.common.utils.PageUtils;
import com.unicom.vw.common.utils.Query;
import com.unicom.vw.common.utils.R;
import com.unicom.vw.modules.sys.dao.GetversionsDao;
import com.unicom.vw.modules.sys.entity.GetversionsEntity;
import com.unicom.vw.modules.sys.service.GetversionsService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/app/getVersions")
public class GetversionsController extends AbstractController {
    private static final Logger logger = LoggerFactory.getLogger(GetversionsController.class);

    @Autowired
    private GetversionsService getversionsService;

    @Autowired
    private GetversionsDao getversionsDao;


    /**
     * 列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("sys:getVersions:list")
    public R list(@RequestParam Map<String, Object> params){
            // 查询列表数据
             Query query = new Query(params);

            List<GetversionsEntity>   getversionsList = getversionsService.queryList(query);

            int total = getversionsService.queryTotal(query);

            PageUtils pageUtil = new PageUtils(getversionsList, total, query.getLimit(), query.getPage());

            return R.ok().put("page", pageUtil);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("sys:getVersions:info")
    public R info(@PathVariable("id") Integer id){
        GetversionsEntity getversions = getversionsService.queryObject(id);

        return R.ok().put("getversions", getversions);
    }

    /**
     * 保存
     */
    @SysLog("保存版本")
    @RequestMapping("/save")
    @RequiresPermissions("sys:getVersions:save")
    public R save(@RequestBody GetversionsEntity getversions){

        Integer value  = getversionsDao.queryName(getversions.getLastVersion(),getversions.getOs());

        if (value==0){

            getversions.setUpdateTime(new Date());

            getversionsService.save(getversions);

            return R.ok();

        }

        return  R.error("error lastVersion exist");

    }

    /**
     * 修改
     */
    @SysLog("修改版本")
    @RequestMapping("/update")
    @RequiresPermissions("sys:getVersions:update")
    public R update(@RequestBody GetversionsEntity getversions){
        getversions.setUpdateTime(new Date());
        getversionsService.update(getversions);
        return R.ok();
    }

    /**
     * 删除
     */
    //@RequestMapping("/delete")
    @SysLog("删除版本")
    @GetMapping(value = "/delete")
    @RequiresPermissions("sys:getVersions:delete")
    public R delete(String ids){
        String stringArray[] = ids.split(",");
        for (String strId:stringArray) {
            Integer id = Integer.parseInt(strId);
            GetversionsEntity getversions = getversionsService.queryObject(id);
            getversions.setSts("1");//0是正常，1是删除
            getversionsService.update(getversions);
        }
        return R.ok();
    }

    /**
     *条件查询
     */
    @GetMapping(value = "/getAll")
    @RequiresPermissions("sys:getVersions:getAll")
    public R getAll(@RequestParam Map<String, Object> params){

        String value=String.valueOf(params.get("sidx"));

        if ("lastVersion".equals(value)){
            params.put("sidx","last_version");
        }
        if ("changeLogZh".equals(value)){
            params.put("sidx","change_log_zh");
        }
        if ("changeLogEn".equals(value)){
            params.put("sidx","change_log_en");
        }
        if ("updateTime".equals(value)){
            params.put("sidx","update_time");
        }
        if ("isForceUpdating".equals(value)){
            params.put("sidx","is_force_updating");
        }

        Query query = new Query(params);

        List<GetversionsEntity>  getversionsList = getversionsService.getAll(query);

        int total = getversionsService.queryTotal_two(query);

        PageUtils pageUtil = new PageUtils(getversionsList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }


}
